Sprocket Menus 2
Volume Number: 11
Issue Number: 6
Column Tag: Getting Started
Sprocket Menus, Part 2
By Dave Mark, MacTech Magazine Regular Contributing Author
Note: Source code files accompanying article are located on MacTech CD-ROM or
source code disks.
Last month, we explored Sprocket’s menu handling mechanism. We took advantage of
the ‘CMNU’ resource to create menus with command numbers attached to each menu
item. We loaded the ‘CMNU’ menus and registered the commands by calling the
TMenuBar classes’ GetMenuFromCMNU() method. We edited the routine
HandleMenuCommand() in the file SprocketStarter.cp to dispatch these commands.
If any of this seems a little hazy, you might want to take a few minutes to review last
month’s column.
Two months ago, we built a TPictureWindow class that implemented a Drag
Manager-friendly PICT window. This month, we’re going to add a new class to our
Drag Manager example. We’ll add a TTextWindow class that is also Drag Manager
friendly. In addition to supporting two different window types, the application will
place a different menu in the menu bar, depending on the type of the front-most
window.
Let’s get started...
Sprocket Resources
We’ll base this month’s program on the Sprocket labeled “Sprocket.02/01/95” and
the SprocketStarter labeled “SprocketDragger.02/01/95”. First make sure you have
both of these folders. Now make a copy of the SprocketDragger folder, calling it
“SprocketPicText.03/25/95”. Since we won’t be making any changes to Sprocket,
there’s no need to make a copy of the Sprocket folder.
Launch your favorite resource editor and open the file StandardMenus.rsrc
inside your Sprocket folder. Copy the ‘CMNU’ resource with an ID of 129 (the one that
implements the File menu), then close StandardMenus.rsrc.
Now go into the SprocketPicText folder and open the resource file
SprocketStarter.rsrc. You’ll be creating all your Sprocket resources in
SprocketStarter.rsrc. If you can avoid it, try not to modify any other Sprocket
resources. At the very least, keep those changes to a minimum. If you can avoid
changing your master Sprocket folder, you’ll be able to get by with a single, Sprocket
folder shared by all your Sprocket applications.
Paste the ‘CMNU’ you copied from StandardMenus.rsrc into
SprocketStarter.rsrc. Change the resource ID from 129 to 1000. Be sure to
change the ID in both places (Get Resource Info from the Resource menu and Edit
Menu & MDEF id from the MENU menu). Wherever possible, you’ll number all
your resource Ids starting at 1000.
Change the first item in this ‘CMNU’ from New to New Text Window and
change the item’s command number (Cmd-Num) to 1000. Insert a new, second item
reading New Picture Window with a command number of 1001. Figure 1 shows a
ResEdit screen shot of the File ‘CMNU’.
Figure 1. The 0 File 0‘CMNU’ resource.
Edit ‘MBAR’ 128, changing the second entry from 129 to 1000. We’ll be
including our own copy of the File ‘CMNU’ in the menu bar instead of the original.
Notice that we did this without making a change to any of the Sprocket resource files.
Duplicate ‘WIND’ 1028, change its ID to 1029 and its window title from Picture
Window to Text Window. This ‘WIND’ will serve as the template for new text windows.
Create a new ‘STR’ resource with an ID of 1000 and containing the text
(without the quotes). This text will appear in the text window
before any text has been dragged into it.
Create two new ‘CMNU’ resources, one with an ID of 1001 and the other with an
ID of 1002. Be sure to change the Ids in both places. ‘CMNU’ 1001 has a title of
Picture and contains two items. Item 1 is Centered, has a check mark next to it and
has a command number of 1001. Item 2 is Upper Left, has no mark next to it, and
has a command number of 1002.
‘CMNU’ 1002 has a title of Text and contains three items. Each of these items
has a submenu. Item 1 is Font, has a command number of 1003, and uses submenu
131. Item 2 is Size, has a command number of 1004, and uses submenu 132. Item 3
is Style, has a command number of 1005, and uses submenu 133. You can find all
three of these submenus in StandardMenus.rsrc. We’ll use them as is.
Source Code: TextWindow.cp
Create a new source code window, save it in the SprocketPicText.03/25/95 folder,
inside the SprocketStarter subfolder, as TextWindow.cp (you’ll find the file
PictWindow.cp in this same folder). Add TextWindow.cp to the project. Here’s the
source code:
const short kTextWindowTemplateID = 1029;
const short kDefaultSTRResID = 1000;
#include "TextWindow.h
#include
MenuHandle TTextWindow::fgMenu;
unsigned long TTextWindow::fgWindowTitleCount = 0;
TTextWindow::TTextWindow()
{
fDraggedTextHandle = nil;
TTextWindow::fgWindowTitleCount++;
this->CreateWindow();
}
TTextWindow::~TTextWindow()
{
}
TTextWindow::MakeNewWindow( WindowPtr behindWindow )
{
WindowPtr aWindow;
Str255 titleString;
GrafPtr savedPort;
GetPort(&savedPort);
aWindow = GetNewColorOrBlackAndWhiteWindow( kTextWindowTemplateID,
nil, behindWindow );
if (aWindow)
{
GetWTitle(aWindow,titleString);
if (StrLength(titleString) != 0)
{
Str255 numberString;
NumToString( fgWindowTitleCount, numberString );
BlockMove(&numberString[1],&titleString[titleString[0]+1],
numberString[0]);
titleString[0] += numberString[0];
}
SetWTitle(aWindow,titleString);
SetPort(aWindow);
ShowWindow(aWindow);
}
SetPort(savedPort);
return aWindow;
}
void
TTextWindow::Draw(void)
{
Rect r;
char *textPtr;
long textLength;
Handle stringH;
r = fWindow->portRect;
EraseRect( &r );
if ( fDraggedTextHandle == nil )
{
stringH = (Handle)GetString( kDefaultSTRResID );